package com.hp.appfw.util;

import javax.mail.internet.MimeMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * Mail Util
 */
public class MailUtil {

	protected final Log logger = LogFactory.getLog(MailUtil.class);

	private JavaMailSender javaMailSender;
	private String from;
	private String encoding = "UTF-8";
	private boolean messageType = true;

	/**
	 * Sync send mail
	 * 
	 * @return If send mail success
	 */
	public boolean send(String title, String content, String[] toEmails) {
		String mailDest = null;

		if(toEmails == null || toEmails.length == 0)
			return false;
		
		if(title == null || title.trim().length() == 0 || content == null || content.trim().length() == 0)
			return false;
		
		for (String ei : toEmails) {
			if (mailDest == null)
				mailDest = ei;
			else
				mailDest += ", " + ei;
		}

		try 
		{
			MimeMessage msg = javaMailSender.createMimeMessage();
			MimeMessageHelper message = new MimeMessageHelper(msg, true, encoding);
			message.setFrom(from);
			message.setSubject(title);
			message.setTo(toEmails);
			message.setText(content, messageType); // set messageType to true to use html

			javaMailSender.send(msg);
			logger.info("Mail send success. Mail Title:" + title + ", Mail To:"
					+ mailDest + ", Mail From:" + this.getFrom());
			return true;
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("Mail send fail. Mail Title:" + title + ", Mail To:"
					+ mailDest + ", Mail From:" + getFrom() + ", Fail Info:"
					+ e.toString());
			return false;
		}
	}	
	public boolean send(String title, String content, String toEmails) {

		if(toEmails == null || toEmails.trim().length() == 0)
			return false;
		
		if(title == null || title.trim().length() == 0 || content == null || content.trim().length() == 0)
			return false;
		
		try 
		{
			MimeMessage msg = javaMailSender.createMimeMessage();
			MimeMessageHelper message = new MimeMessageHelper(msg, true, encoding);
			message.setFrom(from);
			message.setSubject(title);
			message.setTo(toEmails);
			message.setText(content, messageType); // set messageType to true to use html

			javaMailSender.send(msg);
			logger.info("Mail send success. Mail Title:" + title + ", Mail To:"
					+ toEmails + ", Mail From:" + this.getFrom());
			return true;
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("Mail send fail. Mail Title:" + title + ", Mail To:"
					+ toEmails + ", Mail From:" + getFrom() + ", Fail Info:"
					+ e.toString());
			return false;
		}
	}

	public JavaMailSender getJavaMailSender() {
		return javaMailSender;
	}

	public void setJavaMailSender(JavaMailSender javaMailSender) {
		this.javaMailSender = javaMailSender;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public boolean getMessageType() {
		return messageType;
	}

	public void setMessageType(boolean messageType) {
		this.messageType = messageType;
	}
}